package com.tech.koufu.ui.adapter;

import com.inqbarna.tablefixheaders.adapters.BaseTableAdapter;
import com.tech.koufu.R;
import com.tech.koufu.action.OnTableItemClickIf;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class EntrustTableAdapter<T> extends BaseTableAdapter {
	private final LayoutInflater inflater;
	private final static int WIDTH_DIP = 110;
	private final static int HEIGHT_DIP = 48;

	private final Context context;

	private T[][] table;

	private  int width;
	private  int height;
	private OnTableItemClickIf clickIf;
	Resources r ;
	public static CheckBox temBox =null;
	public EntrustTableAdapter(Context context) {
		this(context, null);
	}

	public EntrustTableAdapter(Context context, T[][] table) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		r = context.getResources();
//		Log.d("Tt", "table.length="+table.length+",table[0].length="+table[0].length);
//		width = Math.round(TypedValue.applyDimension(
//				TypedValue.COMPLEX_UNIT_DIP, WIDTH_DIP, r.getDisplayMetrics()));
//		height = Math
//				.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
//						HEIGHT_DIP, r.getDisplayMetrics()));
		width =r.getDimensionPixelSize(R.dimen.table_row_width);  
		height = r.getDimensionPixelSize(R.dimen.table_content_height);
		setInformation(table);
	}

	public void setInformation(T[][] table) {
		this.table = table;
	}
	/**
	 * 数据行
	 */
	@Override
	public int getRowCount() {
		return table.length-1;
	}
	/**
	 * 列
	 */
	@Override
	public int getColumnCount() {
		return table[0].length-1;
	}

	@Override
	public View getView(int row, int column, View convertView, ViewGroup parent) { 
		if (convertView == null) {
			convertView = inflater.inflate(getLayoutResource(row, column),
					parent, false); 
//			Log.e("Tt",row+","+column+"="+getCellString(row, column));
			final int _row = row;
			if(column==-1 && row!=-1){
				final CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.s_checkbox);
				checkbox.setVisibility(View.VISIBLE);
				checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						if(isChecked){
							if(temBox!=null){
								temBox.setChecked(false);
							}
							temBox = checkbox;
						}
						clickIf.OnTableItemClickListener(_row,isChecked); 
						
					}
				});
			}
		 
		}  
		setText(convertView, getCellString(row, column)); 
		return convertView;
	}

	@Override
	public int getHeight(int row) {
		 if(row == -1){
			 height = r.getDimensionPixelSize(R.dimen.table_head_height);
		 }else{
			height = r.getDimensionPixelSize(R.dimen.table_content_height);
		 }
		return height;
	}

	@Override
	public int getWidth(int column) {
		if(column==-1){
			width = r.getDimensionPixelSize(R.dimen.table_column1_width);
		}else{
			width = r.getDimensionPixelSize(R.dimen.table_row_width);
		}
		return width;
	}

	/**
	 * Sets the text to the view.
	 * 
	 * @param view
	 * @param text
	 */
	private void setText(View view, String text) { 
//		if(text==null || TextUtils.isEmpty(text)){
//			return;
//		}
		TextView text1 = ((TextView) view.findViewById(android.R.id.text1));
		TextView text2 = ((TextView) view.findViewById(android.R.id.text2)); 
		 
		text1.setText("");
		
		if(text2!=null){
			text2.setText("");
		}
		if(!TextUtils.isEmpty(text)){ 
			String[] ss = text.split(",");
			text1.setText(ss[0]);
			if(ss[0].startsWith("-")){
				text1.setTextColor(context.getResources().getColor(R.color.btnColorGreen_00C51C));
			}
			if(ss.length>1){  
				text2.setText(ss[1]);
				text2.setVisibility(View.VISIBLE);
			}else{ 
				if(text2!=null){
					text2.setVisibility(View.GONE);
				}
			}
		} 
		 
	}
	 
	public String getCellString(int row, int column) {
		return   (String) table[row+1][column+1];//"Lorem (" + row + ", " + column + ")";
	}
	public int getLayoutResource(int row, int column) {
		final int layoutResource;
		switch (getItemViewType(row, column)) {
		case 0:
			layoutResource = R.layout.item_table_header;
			break;
		case 1:
			layoutResource = R.layout.item_table_column0;
			break; 
		default:
			throw new RuntimeException("wtf?");
		}
		return layoutResource;
	}

	@Override
	public int getItemViewType(int row, int column) { 
		if (row < 0) {
			return 0;
		} else { 
			return 1;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}
	public void setOnItemClick(OnTableItemClickIf itemclick){
		this.clickIf = itemclick;
	}
}
